package nl.saxion.ein4b;

import java.util.ArrayList;

import nl.saxion.ein4b.model.Profile;
import nl.saxion.ein4b.model.ProfileManager;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class ProfileActivity extends ListActivity {
	ProfileManager m;
	ArrayAdapter<String> adapter;
	ArrayList<Profile> profiles;

	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		m = new ProfileManager(this.getBaseContext());
		// ArrayList<Profile> profiles = m.loadProfiles();
		profiles = new ArrayList<Profile>();

		ArrayList<String> profileNames = new ArrayList<String>();
		for (Profile p : m.loadProfiles()) {
			profileNames.add(p.getName() + "    Coins:" + p.getCredit());
		}
		profileNames.add("Nieuw Profiel");
		String[] values = new String[profileNames.size()];
		for (int i = 0; i < profileNames.size() - 1; i++) {
			values[i] = profileNames.get(i);
		}
		values[profileNames.size() - 1] = "Nieuw Profiel";
		// String[] values = (String[]) profileNames.toArray();
		// values = new String[] { "Profiel 1", "Profiel2", "Profiel 3",
		// "Nieuw Profiel" };
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, values);
		setListAdapter(adapter);
	}

	public void onPause() {
		super.onPause();
		finish();
	}

	public void onResume() {
		super.onResume();
		System.out.println("resume van profileactivity");
		/*
		 * //m.load(); ArrayList<String> profileNames = new ArrayList<String>();
		 * for(Profile p : m.loadProfiles()) { profileNames.add(p.getName() +
		 * "    Coins:" + p.getCredit() ); } profileNames.add("Nieuw Profiel");
		 * String[] values = new String[profileNames.size()]; for(int i = 0; i <
		 * profileNames.size()-1;i++) { values[i] = profileNames.get(i); }
		 * values[profileNames.size()-1] = "Nieuw Profiel";
		 * adapter.notifyDataSetChanged();
		 */
	}

	@Override
	protected void onListItemClick(ListView l, final View v, int position,
			long id) {
		String item = (String) getListAdapter().getItem(position);

		if (item.equals("Nieuw Profiel")) {
			AlertDialog.Builder alert = new AlertDialog.Builder(this);

			alert.setTitle("Type your name here");
			// alert.setMessage("Message");

			// Set an EditText view to get user input
			final EditText input = new EditText(this);
			alert.setView(input);

			alert.setPositiveButton("Ok",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							String value = input.getText().toString();
							System.out.println("Nieuw profiel aangemaakt: "
									+ value);
							m.newProfile(new Profile(value, "", 250, 0));
							adapter.notifyDataSetChanged();
							Intent myIntent = new Intent(v.getContext(),
									GameActivity.class);
							System.out.println("Starting game.");
							myIntent.putExtra("profile", m.getProfile(value));
							startActivityForResult(myIntent, 0);
						}
					});

			alert.setNegativeButton("Cancel",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							// Canceled.
						}
					});

			alert.show();
		} else {
			Intent myIntent = new Intent(v.getContext(), GameActivity.class);
			System.out.println("Starting game.");
			myIntent.putExtra("profile", m.getProfile(item));
			startActivityForResult(myIntent, 0);
		}
	}
}
